package app

import (
	"fmt"
	homedir "github.com/mitchellh/go-homedir"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

var Verbose bool
var Source string
var Author string

var (
	// Used for flags.
	cfgFile     string
	userLicense string

	rootCmd = &cobra.Command{
		Use:   "start",
		Short: "start 启动呼叫平台服务",
		Long:  `比扬智慧医护呼叫系统.产品介绍请到公司官网查看 http://www.csbeyond.com`,
		Run: func(cmd *cobra.Command, args []string) {
			// Do Stuff Here
			fmt.Println("root run server start.starting....")
		},
		PersistentPreRun: func(cmd *cobra.Command, args []string) {
			fmt.Printf("Inside rootCmd PersistentPreRun with args: %v\n", args)
		},
		PreRun: func(cmd *cobra.Command, args []string) {
			fmt.Printf("Inside rootCmd PreRun with args: %v\n", args)
		},
		PostRun: func(cmd *cobra.Command, args []string) {
			fmt.Printf("Inside rootCmd PostRun with args: %v\n", args)
		},
		PersistentPostRun: func(cmd *cobra.Command, args []string) {
			fmt.Printf("Inside rootCmd PersistentPostRun with args: %v\n", args)
		},
		TraverseChildren: true,
	}
)

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "查看系统版本",
	Long:  `比扬智慧医护呼叫系统版本信息`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("icare platform v0.9.1")
		fmt.Println("version run args", args)
		fmt.Println("version run args verbose", Verbose)

		author := viper.Get("author")
		fmt.Println("author", author)

		fmt.Println("Source", Source)

	},
}

var versionSubCmd = &cobra.Command{
	Use:   "subversion",
	Short: "查看系统版本",
	Long:  `比扬智慧医护呼叫系统版本信息`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("icare platform v0.9.1")
		fmt.Println("subversion 1")
	},
}

func init() {
	cobra.OnInitialize(initConfig)

	// viper负责从配置文件中读取

	// v flag将在rootCmd及以下的子命令上都生效
	rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")

	// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
	// 创建一个全局命令的flag参数,并将用户输入绑定到变量
	rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.cobra.yaml)")
	rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
	rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
	rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")

	// 设为必填参数
	//rootCmd.MarkPersistentFlagRequired("author")
	// rootCmd.PersistentFlags().Lookup("author") 获取一个PFlag对象
	viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))  // 使用viper绑定标志，Viper绑定pflag对象
	viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper")) // 绑定之后viper的配置数据来源，变为从命令行来获取
	viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
	viper.SetDefault("license", "apache")

	// 该命令只在localCmd上起作用
	versionCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")

	rootCmd.AddCommand(versionCmd)
	versionCmd.AddCommand(versionSubCmd)
	//rootCmd.AddCommand(addCmd)
	//rootCmd.AddCommand(initCmd)

}

func initConfig() {
	fmt.Println("cfg file", cfgFile)
	if cfgFile != "" {
		// Use config file from the flag.
		viper.SetConfigFile(cfgFile)
	} else {
		// Find home directory.
		home, err := homedir.Dir()
		cobra.CheckErr(err)

		// Search config in home directory with name ".cobra" (without extension).
		viper.AddConfigPath(home)
		viper.SetConfigName(".cobra")
	}

	viper.AutomaticEnv()

	if err := viper.ReadInConfig(); err == nil {
		fmt.Println("Using config file:", viper.ConfigFileUsed())
	}
}

func Execute() error {
	return rootCmd.Execute()
}
